package org.ofbiz.wvtools.wxutil;

import java.util.Map;

import javolution.util.FastMap;

import org.ofbiz.base.util.Debug;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;

import com.alibaba.fastjson.JSONObject;

public class WXServices {
	public static String module = WXServices.class.getName();
	public static int Integrals = 100;
	

	/**
	 * @return net.sf.json.JSONObject
	 * @author LZL
	 * @Description通过code获取openId和sessionKey方法
	 * @Date 18:03 2018/12/11
	 * @Param [code]
	 **/
	public static Map<String, Object> getSessionKeyAndOpenId(DispatchContext ctx, Map<String, ? extends Object> context) {
		// 微信端登录code
		Map<String, Object> result = ServiceUtil.returnSuccess();
		Map<String, Object> nfs = FastMap.newInstance();
		String code = (String) context.get("code");
		try {
			GzhConfig config = new GzhConfig("");	
			String appId = config.APPID;
			String appSecuret = config.APPSECRET;
			String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
			// 发送post请求读取调用微信接口获取openid用户唯一标识
			JSONObject jsonObject = JSONObject.parseObject(HttpUtils.sendPost(requestUrl, "appid=" + appId + "&secret="
					+ appSecuret + "&js_code=" + code + "&grant_type=authorization_code"));
			String session_key = (String) jsonObject.get("session_key");
			String openid = (String) jsonObject.get("openid");
			Debug.log("session_key == " + session_key);
			Debug.log("openid == " + openid);
			nfs.put("session_key", session_key);
			nfs.put("openid", openid);
			result.put("nfs", nfs);
		} catch (Exception e) {
			Debug.log(e.getMessage());
		}

		return result;
	}
}
